<?php
/**
 * Introduction:  优惠卷逻辑
 * @author: 杨陈鹏
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Common\Logic;

use Common\Model;

class VolumeLogic
{

    public static $model;

    public function __construct()
    {

        self::$model = new Model\VolumeModel();

    }
    /**
     * Introduction:  添加一张优惠卷
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function create($data)
    {

        $setting_logic          = new SettingLogic();

        $discount               = $setting_logic::get('discount');

        $discount               = $discount['data'];

        $data['price']          = intval(floatval($discount['price'] * 100));

        $data['end_time']       = NOW_TIME + (intval($discount['time']) * 60 * 60 *24);

        $data['create_time']    = NOW_TIME;
        $data['from_time']      = NOW_TIME;
        $data['from_id']        = 0;

        $data['category']       =   $discount['category'] ? intval($discount['category']) : 0 ;

        //为0则不发放优惠卷
        if(!$data['price'] || !intval($discount['time']) || !$discount['nums'])
            return false;

        if(self::$model->add($data)){

            $discount['nums']   -= 1;
            $setting_logic::set('discount',$discount,'优惠卷');

            return true;
        }

        return false;


    }

    /**
     * Introduction:  获取优惠卷列表
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function getData($where,$page,$nums,$field = '*',$checkout,$goods_ids)
    {

        $member_logic   =   new MemberLogic();
        $category_logic =   new CategoryLogic();

        $data = self::$model
            ->where($where)
            ->page($page,$nums)
            ->field($field)
            ->order('create_time desc')
            ->select();

        if ( $checkout ) {

            //判断当前商品是否是通一个分类
            $goods_ids = trim($goods_ids,',');
            $sql = "
              SELECT category_id
                FROM goods
               WHERE id IN ($goods_ids)
            GROUP BY category_id
        ";
            $result = M()->query($sql);


            if ( count($result) == 1 ) {

                foreach($data as $k => $v){

                    $data[$k]['is_use'] = 2;
                    if (!$v['category'] || $v['category'] == $result[0]['category_id'] )
                        $data[$k]['is_use'] = 1;

                    $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);
                    $data[$k]['end_time'] = date('Y-m-d',$v['end_time']);

                    if ( $v['from_id'] ){

                        $mobile             =  $member_logic::$model
                            ->where(['uid' => $v['from_id']])
                            ->getField('mobile');
                        $data[$k]['from']   =  '来着于用户:'.$mobile;

                    } else {

                        $data[$k]['from']   =   '来自于平台!';

                    }

                    if($v['category']){
                        $data[$k]['category'] = $category_logic::$model
                            ->where(['id' => $v['category']])
                            ->getField('name');
                    } else {
                        $data[$k]['category'] = '全场通用';
                    }

                }
                return ['code' => 200 ,'data' => $data];
            }  else {

                foreach($data as $k => $v){

                    $data[$k]['is_use'] = 2;
                    if (!$v['category'])
                        $data[$k]['is_use'] = 1;

                    $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);
                    $data[$k]['end_time'] = date('Y-m-d',$v['end_time']);


                    if ( $v['from_id'] ){

                        $mobile             =  $member_logic::$model
                            ->where(['uid' => $v['from_id']])
                            ->getField('mobile');
                        $data[$k]['from']   =  '来着于用户:'.$mobile;

                    } else {

                        $data[$k]['from']   =   '来自于平台!';

                    }

                    if($v['category']){
                        $data[$k]['category'] = $category_logic::$model
                            ->where(['id' => $v['category']])
                            ->getField('name');
                    } else {
                        $data[$k]['category'] = '全场通用';
                    }


                }
                return ['code' => 200 ,'data' => $data];

            }


        } else {

            if ( !$data )
            {

                if($page == 1)
                    return ['code' => 300,'msg' => '暂无数据!'];

                return ['code' => 200,'msg' => '后面没有啦!'];

            }


            foreach($data as $k => $v){

                $data[$k]['create_time'] = date('Y-m-d',$v['create_time']);
                $data[$k]['end_time'] = date('Y-m-d',$v['end_time']);


                if ( $v['from_id'] ){

                    $mobile             =  $member_logic::$model
                        ->where(['uid' => $v['from_id']])
                        ->getField('mobile');
                    $data[$k]['from']   =  '来着于用户:'.$mobile;

                } else {

                    $data[$k]['from']   =   '来自于平台!';

                }

                if($v['category']){
                    $data[$k]['category'] = $category_logic::$model
                        ->where(['id' => $v['category']])
                        ->getField('name');
                } else {
                    $data[$k]['category'] = '全场通用';
                }


            }

            return ['code' => 200 ,'data' => $data];
        }

    }

}